﻿$(document).ready(function() {
    var arrowPosition = 0;
    $(".searchPopupOut").click(function() {
        $(".popup").slideToggle("slow");
        if (arrowPosition) {
            $("#searchArrow").removeClass("searcharrowUp");
            $("#searchArrow").addClass("searcharrowDwn");
            arrowPosition++;
        }
        else {
            $("#searchArrow").removeClass("searcharrowDwn");
            $("#searchArrow").addClass("searcharrowUp");
            arrowPosition--;
        }
    });

    $("#volunteersRequired").click(function() {
        $("#query").val("Volunteers: Required");
        search("Volunteers", $("#volunteersRequired").attr("checked"), 0);
    });
    $("#volunteersNotRequired").click(function() {
        $("#query").val("Volunteers: Not-Required");
        search("Volunteers", !$("#volunteersNotRequired").attr("checked"), 0);
    });

    $(".closeCreteria").click(function() {
        $(".popup").slideToggle("slow");
        $("#searchArrow").removeClass("searcharrowUp");
        $("#searchArrow").addClass("searcharrowDwn");
        arrowPosition++;
    });

    $("#topNContainer").bind('keypress', function(e) {
        var evt = (e) ? e : (window.event) ? window.event : null;
        if (evt) {
            var key = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
        }
        if (key >= 48 && key <= 57 || key == "8") {
        }
        else {
            if (key == 13) {
                if ($("#topN").val() == "") {
                    return;
                }
                $("#query").val("Top: " + $("#topN").val());
                search("Top", $("#topN").val(), 0);
            }
            else {
                return false;
            }
        }
    });

    $("#query").bind('keypress', function(e) {
        if (e.keyCode == 13) {
            if ($("#query").val() == "") {
                return;
            }
            var searchQuery = new String($("#query").val());
            searchQuery = searchQuery.replace("SearchText: ", "").replace("Status: ", "").replace("Top: ", "").replace("Volunteers: ", "");
            search("SearchText", searchQuery, 0);
        }
    });

    function search(criteria, query, pageIndex) {
        var page = 1;
        window.location = "/Projects/Search/" + criteria.toString() + "/" + query.toString();
    }

    $("#status").change(function() {
        if ($(this).val() != "Select") {
            $("#query").val("Status: " + $(this).val());
            search("Status", $(this).val(), 0);
        }
    });

    $(".searchImg").click(function() {
        if ($("#query").val() == "") {
            return;
        }
        var searchQuery = new String($("#query").val()); ;
        searchQuery = searchQuery.replace("SearchText: ", "").replace("Status: ", "").replace("Top: ", "");
        search("SearchText", searchQuery, 0);
    });

});